<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @subpackage Records
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: RGB.php 5 2010-02-14 05:08:39Z indigo.sunrise $
 */

/**
 * Represents an RGB color record.
 *
 * @package    Pwiff
 * @subpackage Records
 */
class Pwiff_Record_RGB extends Pwiff_Record
{
	/**
	 * Red color value.
	 *
	 * @var int
	 */
	protected $red = 0;
	
	/**
	 * Green color value.
	 *
	 * @var int
	 */
	protected $green = 0;
	
	/**
	 * Blue color value.
	 *
	 * @var int
	 */
	protected $blue = 0;
	
	/**
	 * Sets the red, green, and blue component colors.
	 *
	 * @param  int $red
	 * @param  int $green
	 * @param  int $blue
	 */
	public function setRGB($red, $green, $blue)
	{
		$this->setRed($red);
		$this->setGreen($green);
		$this->setBlue($blue);
	}
	
	/**
	 * Sets the red color.
	 *
	 * @param  int $red
	 * @return Pwiff_Record_RGB
	 */
	public function setRed($red)
	{
		if ($red < 0 OR $red > 255) {
			throw new Pwiff_Exception('Red value must be between 0 and 255.');
		}
		$this->red = $red;
		return $this;
	}
	
	/**
	 * Sets the green color.
	 *
	 * @param  int $green
	 * @return Pwiff_Record_RGB
	 */
	public function setGreen($green)
	{
		if ($green < 0 OR $green > 255) {
			throw new Pwiff_Exception('Green value must be between 0 and 255.');
		}
		$this->green = $green;
		return $this;
	}
	
	/**
	 * Sets the blue color.
	 *
	 * @param  int $blue
	 * @return Pwiff_Record_RGB
	 */
	public function setBlue($blue)
	{
		if ($blue < 0 OR $blue > 255) {
			throw new Pwiff_Exception('Blue value must be between 0 and 255.');
		}
		$this->blue = $blue;
		return $this;
	}
	
	/**
	 * Returns the RGB values as a 3 element array.
	 *
	 * @return array
	 */
	public function getRGB()
	{
		return array($this->red, $this->green, $this->blue);
	}
	
	/**
	 * Returns the red value.
	 *
	 * @return int
	 */
	public function getRed()
	{
		return $this->red;
	}
	
	/**
	 * Returns the green value.
	 *
	 * @return int
	 */
	public function getGreen()
	{
		return $this->green;
	}
	
	/**
	 * Returns the blue value.
	 *
	 * @return int
	 */
	public function getBlue()
	{
		return $this->blue;
	}
	
	/**
	 * Returns the record length. Always 3.
	 *
	 * @return int
	 */
	public function getRecordLength()
	{
		return 3;
	}
	
	/**
	 * Writes the RGB values to the given output instance.
	 *
	 * @param  Pwiff_Output $out
	 */
	public function write(Pwiff_Output $out)
	{
		$out->writeUI8($this->red);
		$out->writeUI8($this->green);
		$out->writeUI8($this->blue);
	}
}
